<?php

namespace addons\podunk\model;

use think\Model;
use app\common\library\Auth;
use think\Db;
use think\Log;
use fast\Random;
use addons\podunk\exception\Exception;

/**
 * 订单模型
 */
class Order extends Model
{
    protected $name = 'podunk_order';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
    ];

    public static function getOrderList($app, $search)
    {
        if($search){
            $where = [
                'app'=>['LIKE',$search."%"],
                'type'=>['LIKE',$search."%"],
                'mobile'=>['LIKE',$search."%"],
                'address'=>['LIKE',$search."%"],
                'consignee'=>['LIKE',$search."%"],
                'product_name'=>['LIKE',$search."%"],
            ];
        }else{
            $where = [];
        }
        $orderList = self::where(['user_id'=>Auth::instance()->id, 'app'=>$app])->with('device')->where(function($query) use($where){
            $query->whereOr($where);
        })->order('id desc')->paginate(10);
        if($orderList){
            return $orderList;
        }
        return null;
    }

    public function getScreenshotAttr($value){
        return 'http://8.136.131.81'.$value;
    }

    public function device()
    {
        return $this->belongsTo('Device', 'device_id', 'id');
    }


}
